package designmodel;


/***
 * 简单策略模式的实现
 */
public class StrategyPattern {

    public static void main(String[] args) {
        StrategyA strategyA = new StrategyA();
        StrategyB strategyB = new StrategyB();
        Context context = new Context();
        context.setStrategy(strategyA);
        context.strategyMethod();

        context.setStrategy(strategyB);
        context.strategyMethod();

    }



}

// 抽象策略模式
interface  Strategy {
    public void strategyMethod();
}

class StrategyA implements Strategy {

    @Override
    public void strategyMethod() {
        System.out.println("StrategyA策略执行");
    }
}

class StrategyB implements Strategy {

    @Override
    public void strategyMethod() {
        System.out.println("StrategyB策略执行");
    }
}

class Context {
    private Strategy strategy;

    void strategyMethod() {
        strategy.strategyMethod();
    }

    public Strategy getStrategy() {
        return strategy;
    }

    public void setStrategy(Strategy strategy) {
        this.strategy = strategy;
    }
}
